$(function () {

// 查询
    $("#btn_sskcdetail_search").click(function () {
        $('#grid_xsmx_sskc').datagrid('load',
            {
                xszxh: $('#xszxh').val(),
                carNo: $('#carNo').val(),
                bindNo: $("#bindNo").val()
            }
        );
    });

    //表格初始加载
    $('#grid_xsmx_sskc').datagrid({
        url: $.webPath() + '/xsgl/xsmx/sskcDetailList?kclx=' + $("#kclx").val() + '&xshtId=' + $("[name='xsht_name']").val() || '',
        fitColumns: false,
        singleSelect: true,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter: true,
        toolbar: "#toolbar_xsmx_sskc",
        frozenColumns: [
            [
                {field: 'checked', checkbox: true},
                {field: 'wzgg.id', title: '物资编码ID', hidden: true},
                {field: 'wzgg.code', title: '物资编码', hidden: true},
                {field: 'xszxh', title: '合同子项号', width: 85},
                {field: 'wzgg.name', title: '品名', width: 100},
                {field: 'wzgg.cz', title: '材质', width: 85},
                {field: 'wzgg.ggxh', title: '规格', width: 85},
                {field: 'carNo', title: '车皮号', width: 85},
                {field: 'bindNo', title: '捆包号', width: 85},
                {field: 'ck', title: '长宽', editor: {type: 'text'}, hidden: true},
                {field: 'wzgg.dw', title: '单位', width: 30}
            ]
        ],
        columns: [
            [
                {field: 'rkrq', title: '入库日期', width: 70},
                {field: 'ckrq', title: '出库日期', width: 70},
                {
                    field: 'js', title: '件数',
                    formatter: function (value) {
                        var js = formatSlForHtml(value, ",");
                        return js;
                    }, width: 70
                },
                {
                    field: 'rksl', title: '入库数量（吨）',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 100
                },
                {
                    field: 'sbsl', title: '剩余数量（吨）',
                    formatter: function (value) {
                        var rksl = formatSlForHtml(value, ",");
                        return rksl;
                    }, width: 100
                },
                {
                    field: 'hsj', title: '合同单价(含税)',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 100
                },
                {
                    field: 'bhsj', title: '合同单价(不含税)',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 100
                }
            ]
        ],
        loadFilter: function (data) {
            $.each(data.rows, function () {
                var bhsj = Math.round(this.bhsj * 10000) / 10000;
                var hsj = Math.round(this.hsj * 10000) / 10000;
                var sl = Math.round(this.sbsl * 10000) / 10000;

                if (this.bhsj) {
                    this.bhse = Math.round(multiply(sl, bhsj) * 100) / 100;
                }
                if (this.hsj) {
                    this.hse = Math.round(multiply(sl, hsj) * 100) / 100;
                }
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
        },
        onLoadSuccess: function () {
            countSumXsd();
        }
    });
})

//合计计算
function countSumXsd() {
    var sumjs = 0;
    var sumsbsl = 0;
    var sumrksl = 0;
    var data = $("#grid_xsmx_sskc").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var js = parseFloat(data.rows[i]["js"]);
        var sbsl = parseFloat(data.rows[i]["sbsl"]);
        var rksl = parseFloat(data.rows[i]["rksl"]);
        js = isNaN(js) ? 0 : js;
        sbsl = isNaN(sbsl) ? 0 : sbsl;
        rksl = isNaN(rksl) ? 0 : rksl;
        sumjs += js;
        sumsbsl += sbsl;
        sumrksl += rksl;
    }
    addFooter(sumjs, sumrksl, sumsbsl);

}

function addFooter(sumjs, sumrksl, sumsbsl) {
    var footer = null;
    footer = [
        {
            "wzgg.dw": "合计", "js": formatSl(sumjs), "rksl": formatSl(sumrksl),
            "sbsl": formatSl(sumsbsl)
        }
    ];
    if (footer != null)
        $("#grid_xsmx_sskc").edatagrid('reloadFooter', footer);
}